﻿<%@ WebHandler Language="C#" Class="general" %>

using System;
using System.Web;
using System.Web.SessionState;

using Newtonsoft.Json;
using VISS.Web.Frontend;
using NCT.Framework;

public class general : IHttpHandler, IRequiresSessionState {

    private readonly static string FromAddress = "viss.vn.gopy@gmail.com";
    private readonly static string SenderName = "VISS.VN - Thư Góp Ý";
    private readonly static string ToAddress = "hotro@viss.vn";
    
    private delegate void DelSendMail(UserContext userContext, string content);
    private void SendMail(UserContext userContext, string content) {
        
        string body = string.Empty;
        body += "----------------------Thông tin thành viên----------------------<br />";
        body += "<p>";
        body += "UserKey: " + userContext.UserKey + "<br />";
        body += "Họ tên: " + userContext.FullName + "<br />";
        body += "Email: " + userContext.Email + "<br />";
        body += "</p>";
        body += "----------------------Nội dung góp ý----------------------<br />";
        body += "<p>";
        body += content;
        body += "</p>";

        MailUtils.SendMail(SenderName, body, FromAddress, SenderName, ToAddress, true, true);
    }
    
    public void ProcessRequest (HttpContext context) {
        string action = context.Request.Form["action"];
        
        switch(action) {
            case "sendCommentToAdmin":
                SendCommentToAdmin(context);
                break;
        }
    }
    
    private void SendCommentToAdmin(HttpContext context) {
        string result = "{{\"result\":\"{0}\"}}";
        UserContext userContext = LoginFactory.GetCurrentUser();
        if(userContext == null) {
            FormValidateUtils.Return(string.Format(result, "-1"));
            return;
        }

        string content = ConvertUtils.ToString(context.Request.Form["content"]);
        if(content.Length == 0) {
            FormValidateUtils.Return(string.Format(result, "2"));
            return;
        }

        DelSendMail del = new DelSendMail(SendMail);

        del.Invoke(userContext, content);

        FormValidateUtils.Return(string.Format(result, "1"));
        return;
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}